package teacher

import (
	"anban/controllers"
	"anban/logic"
	"anban/service"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
)

type UserController struct {
	controllers.BaseController
}

func (c *UserController) Index() {
	userId := c.GetSession("userId")
	p := map[string]interface{}{}
	p["id"] = userId
	p["convert"] = true
	c.Data["userInfo"] = service.GetUserInfo(p)

	c.Data["contact"] = beego.AppConfig.String("contact")
	c.Data["title"] = "用户中心"
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "teacher/user-index.html"
}

func (c *UserController) MyWallet() {
	userId := c.GetSession("userId")
	p := map[string]interface{}{}
	p["id"] = userId
	c.Data["userInfo"] = service.GetUserInfo(p)

	c.Data["title"] = "我的钱包"
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "teacher/my-wallet.html"
}

func (c *UserController) Withdraw() {
	userId := c.GetSession("userId")
	p := map[string]interface{}{}
	p["id"] = userId
	c.Data["userInfo"] = service.GetUserInfo(p)

	c.Data["title"] = "提现"
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.TplName = "teacher/user-withdraw.html"
}

func (c *UserController) WithdrawLoadMore() {
	count, _ := beego.AppConfig.Int("loadcount")
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	p := map[string]interface{}{}
	p["convert"] = true
	p["userId"] = c.GetSession("userId")
	p["status"] = 1
	p["curPage"] = curPage
	p["perCount"] = count
	_, c.Data["withdrawList"] = service.GetWithdrawRecordList(p)
	c.TplName = "teacher/withdraw-list.html"
}

func (c *UserController) DoWithdraw() {
	var res *utils.ResJsonStruct
	input := c.Input()

	userId := c.GetSession("userId")
	input["user_id"] = []string{fmt.Sprintf("%d", userId)}

	err := logic.Withdraw(input)
	if err != nil {
		res = utils.ResJson(0, err.Error(), "")
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res = utils.ResJson(1, "提现成功", "")
	c.Data["json"] = res
	c.ServeJSON()
}
